package org.proddevplm.dao.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.proddevplm.dao.api.IUserMgmt;
import org.proddevplm.dao.data.Project;
import org.proddevplm.dao.data.RelationMatrix;
import org.proddevplm.dao.data.Roles;
import org.proddevplm.dao.data.User;
import org.proddevplm.dao.util.HibernateUtil;

class UserMgmt implements IUserMgmt {

	public int addUser(User user) {
		
		
		HibernateUtil.currentSession().persist(user);
		
		
		return user.getId();
	}

	public boolean updateUser(int userId, User user) {
		return false;
	}

	public boolean removeUser(int userId) {
		return false;
	}

	public User getUser(int userId) {
		Session session = HibernateUtil.currentSession();
		Query q = session.createQuery("select u from User u where u.id=:userId");
		q.setInteger("userId", userId);
		return (User)q.uniqueResult();
	}

	public List<User> getUsers() {
		Session session = HibernateUtil.currentSession();
		Query q = session.createQuery("select u from User u where u.category=:category");
		q.setString("category", "devTeam");
		return q.list();
	}
	
	public Map<Integer, User> getUsersById(String[] usersArray){
		
		Map<Integer, User> usersMap = new HashMap<Integer, User>();
		Session session = HibernateUtil.currentSession();
		List<Integer> userIds = new ArrayList<Integer>();
		for (String str: usersArray) {
			   userIds.add(Integer.valueOf(str));
			}
		String query="select user from User user where user.id in (:userIds)";
		Query q = session.createQuery(query);
		q.setParameterList("userIds", userIds); 
    	
    	List<User> results = q.list();
    	for (User user: results){
    		usersMap.put(user.getId(), user);
    	}
    		
    	
		return usersMap;
	}

	public User login(String username, String password) {
		Session session = HibernateUtil.currentSession();
		Query query = session.createQuery("select u from User as u where u.username=:username and u.password=:password");
		query.setString("username", username);
		query.setString("password", password);
		User user = (User)query.uniqueResult();
		
		return user;
	}

	public List<String[]> getUserNames() {
		Session session = HibernateUtil.currentSession();
		List<User> userResult = session.createQuery("select u from User as u").list();
		List<String[]> userNames = new ArrayList<String[]>();
		for (User u:userResult){
			userNames.add( new String[] { u.getId()+"", u.getSurname()+" "+u.getName() } );
		}
		return userNames;
	}

	@Override
	public List<User> getUsersByProjectId(int projectId) {
		Session session = HibernateUtil.currentSession();
		Query q = session.createQuery("select r from RelationMatrix r where r.project.id=:projectId");
		q.setInteger("projectId", projectId);
		List<RelationMatrix> matrixResults = q.list();
		List<User> userResults = new ArrayList<User>(matrixResults.size());
		
		for (RelationMatrix r:matrixResults)
			userResults.add(r.getUser());
		
		
		return userResults;
	}

	@Override
	public boolean getUsersByUsername(String username) {
		
		Session session = HibernateUtil.currentSession();
		Query q = session.createQuery("select u from User u where u.username=:username");
		q.setParameter("username", username);
		List<User> userResults = q.list();
		
		if (userResults.size()!=0) return true;
		else return false;
	}

	@Override
	public boolean addMemberOnProject(Project project, User user, Roles role) {
		RelationMatrix relMatrix = new RelationMatrix();
		relMatrix.setProject(project);
		relMatrix.setRole(role);
		relMatrix.setUser(user);
		HibernateUtil.currentSession().persist(relMatrix);
		return false;
	}

	@Override
	public boolean removeUserFromProject(int projectId, int userId, int roleId) {
		
		Session session = HibernateUtil.currentSession();
		Query q = session.createQuery("delete from RelationMatrix r where r.project.id=:projectId and r.user.id=:userId and r.role.id=:roleId");
		q.setInteger("projectId", projectId);
		q.setInteger("userId", userId);
		q.setInteger("roleId", roleId);
		q.executeUpdate();
		return false;
	}
	
}
